package ma.fizazi.missionsmanagement.presentation.controllers;

import java.security.Principal;
import java.util.List;

import ma.fizazi.missionsmanagement.business.activites.ActiviteBusiness;
import ma.fizazi.missionsmanagement.business.commons.CommonBusiness;
import ma.fizazi.missionsmanagement.business.utilisateurs.UtilisateurBusiness;
import ma.fizazi.missionsmanagement.model.Activite;
import ma.fizazi.missionsmanagement.model.Utilisateur;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/activites")
public class ActiviteController {

	@Autowired
	CommonBusiness commonBusiness;

	@Autowired
	ActiviteBusiness activiteBusiness;

	@Autowired
	UtilisateurBusiness utilisateurBusiness;


	@RequestMapping(method = RequestMethod.GET)
	public String activitesPage(ModelMap model) {

		return "activites";
	}
	
	@RequestMapping(value="/all", method = RequestMethod.GET)
	public String allactivitesPage(ModelMap model) {
		return "allActivites";
	}
	
//	@RequestMapping(value = "/test", method = RequestMethod.GET)
//	public @ResponseBody
//	void test(ModelMap model, Authentication a) {
//		String auths = a.getAuthorities().toString();
//		System.out.println(auths.contains("Directeur"));
//	}

	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(BindingResult result, SessionStatus status) {

		return "redirect:/activites";
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String newActiviteModal(ModelMap model) {

		return "newActivite";
	}

	@RequestMapping(value = "/new", method = RequestMethod.POST)
	public String newActiviteSubmit(
			@ModelAttribute("newActivite") Activite newActivite,
			ModelMap model, Principal principal) {

		Utilisateur user = (Utilisateur) utilisateurBusiness.getUserByFullName(principal.getName());
		newActivite.getUtilisateurs().add(user);
		commonBusiness.saveEntity(newActivite);
		return "redirect:/activites";
	}

	@ModelAttribute("newActivite")
	public Activite getNewActivite() {
		return new Activite();
	}

	@ModelAttribute("mesActivites")
	public List<Activite> getMyActivities(Principal principal, Authentication a) {
		String name = principal.getName();
		if ( a.getAuthorities().toString().contains("Administrateur")||"admin".equals(name))
		{
		return activiteBusiness.getAllActivites();	
		}
		else
		{
		return activiteBusiness.getAllActivitesByUser(name);
		}
	}

	
	@ModelAttribute("utilisateurs")
	public List<Utilisateur> getUtilisateurs() {
		List<Utilisateur> utilisateurs = utilisateurBusiness
				.getAllConsultants();
		return utilisateurs;
	}
	
}
